#pragma once
#include <iostream>
#include <string>
#include <vector>
#include "../Common/Log.hpp"
#include "../Common/Util.hpp"
#include <ctemplate/template.h>
#include "Model.hpp"

namespace ns_view
{
    using namespace ns_model;
    static const std::string template_path = "./ctemplate_html/";
    class View
    {
    public:
        View() {}
        ~View() {}
        void SetALLQuestionHtml(const std::vector<Question> &res, std::string *html)
        {
            std::string path = template_path + "all_questions.html";
            ctemplate::TemplateDictionary root("all_questions");
            for (auto &x : res)
            {
                // 获取子字典
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("question_list");
                sub->SetValue("number", x._id);
                sub->SetValue("title", x._title);
                sub->SetValue("level", x._level);
            }

            // 渲染
            ctemplate::Template *td = ctemplate::Template::GetTemplate(path, ctemplate::DO_NOT_STRIP);
            td->Expand(html, &root);
        }
        void SetOnceQuestionHtml(const Question &q, std::string *html)
        {
            std::string path = template_path +  "once_question.html";
            ctemplate::TemplateDictionary root("once_question");
            root.SetValue("number",q._id);
            root.SetValue("title",q._title);
            root.SetValue("level",q._level);
            root.SetValue("cpu_limit",std::to_string(q._cpu_limit));
            root.SetValue("mem_limit",std::to_string(q._mem_limit));
            root.SetValue("desc",q._desc);
            root.SetValue("header",q._header);
            root.SetValue("tail",q._tail);
            ctemplate::Template *td = ctemplate::Template::GetTemplate(path, ctemplate::DO_NOT_STRIP);
            td->Expand(html, &root);
        }
    };
}
